<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="None">
        
        
        <link rel="shortcut icon" href="img/favicon.ico">
        <title>Web3.js 0.20官方文档中文版</title>
        <link href="css/bootstrap-custom.min.css" rel="stylesheet">
        <link href="css/font-awesome.min.css" rel="stylesheet">
        <link href="css/base.css" rel="stylesheet">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css">
        <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!--[if lt IE 9]>
            <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
            <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->

        <script src="js/jquery-1.10.2.min.js" defer></script>
        <script src="js/bootstrap-3.0.3.min.js" defer></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
        <script>hljs.initHighlightingOnLoad();</script> 
    </head>

    <body class="homepage">

        <div class="navbar navbar-default navbar-fixed-top" role="navigation">
            <div class="container">

                <!-- Collapsed navigation -->
                <div class="navbar-header">
                    <!-- Expander button -->
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href=".">Web3.js 0.20官方文档中文版</a>
                </div>

                <!-- Expanded navigation -->
                <div class="navbar-collapse collapse">
                        <!-- Main navigation -->
                        <ul class="nav navbar-nav">
                            <li >
                                <a href="https://web3.learnblockchain.cn">首页</a>
                            </li>
                            <li class="active">
                                <a href=".">0.20文档</a>
                            </li>
                            <li >
                                <a href="https://learnblockchain.cn">深入浅出区块链</a>
                            </li>
                            <li >
                                <a href="https://wiki.learnblockchain.cn/images/dapp.png">DAPP视频课程</a>
                            </li>
                        </ul>

                    <ul class="nav navbar-nav navbar-right">
                        <li>
                            <a href="#" data-toggle="modal" data-target="#mkdocs_search_modal">
                                <i class="fa fa-search"></i> Search
                            </a>
                        </li>
                    </ul>
                </div>
            </div>
        </div>

        <div class="container">
                <div class="col-md-3"><div class="bs-sidebar hidden-print affix well" role="complementary">
    <ul class="nav bs-sidenav">
        <li class="main active"><a href="#web3js-02xx-api">Web3.js 0.2x.x API 中文版手册</a></li>
            <li><a href="#web3">引入web3</a></li>
            <li><a href="#_1">使用回调</a></li>
            <li><a href="#_2">批量请求</a></li>
            <li><a href="#web3js">web3.js中的大数处理</a></li>
        <li class="main "><a href="#web3js-api">Web3.js API列表</a></li>
            <li><a href="#web3_1">web3</a></li>
            <li><a href="#web3net">web3.net</a></li>
            <li><a href="#web3eth">web3.eth</a></li>
            <li><a href="#web3db">web3.db</a></li>
            <li><a href="#web3shh">web3.shh</a></li>
    </ul>
</div></div>
                <div class="col-md-9" role="main">

<h1 id="web3js-02xx-api">Web3.js 0.2x.x API 中文版手册</h1>
<p><strong> 注意本文档是: web3.js 0.2x.x版本，如果你使用的是 web3.js 1.0，请参考<a href="http://web3js.readthedocs.io/en/1.0/index.html">文档</a>.</strong></p>
<p>为了开发一个基于以太坊的去中心化应用程序，可以使用<a href="https://github.com/ethereum/web3.js">web3.js库</a>提供的web3对象， 在底层实现上，web3通过<a href="https://github.com/ethereum/wiki/wiki/JSON-RPC">RPC调用</a>与本地节点通信， web3.js可以与任何暴露了RPC接口的以太坊节点连接。</p>
<p><code>web3</code> 包含下面几个对象：</p>
<ul>
<li><code>web3.eth</code> 用来与以太坊区块链及合约的交互</li>
<li><code>web3.shh</code> 用来与Whisper协议相关交互</li>
<li><code>web3.net</code> 用来获取网络相关信息</li>
<li><code>web3</code>     包含一些工具</li>
</ul>
<p>web3使用示例：</p>
<ul>
<li><a href="https://web3.learnblockchain.cn/transDemo.html">使用web3.js API在页面中转账</a></li>
<li><a href="https://web3.learnblockchain.cn/ContractEventDemo.html">使用web3.js 与合约交互及监听合约事件</a></li>
<li><a href="https://web3.learnblockchain.cn/web3_v0.20_demo.html">多个API 使用Demo</a></li>
<li><a href="https://github.com/ethereum/web3.js/tree/master/example">官方示例</a></li>
<li><a href="https://github.com/ethereum/wiki/wiki/Useful-Ðapp-Patterns">Dapp 模式</a></li>
</ul>
<p>想要学习去中心化应用(DAPP)开发，这门课程不容错过<a href="https://wiki.learnblockchain.cn/course/dapp.html">区块链全栈-以太坊DAPP开发实战</a></p>
<h2 id="web3">引入web3</h2>
<p>首先你需要将web3引入到应用工程中，可以通过如下几个方法：</p>
<ul>
<li>npm: <code>npm install web3</code></li>
<li>bower: <code>bower install web3</code></li>
<li>meteor: <code>meteor add ethereum:web3</code></li>
<li>vanilla: link the <code>dist/web3.min.js</code></li>
</ul>
<p>然后你需要创建一个web3的实例，设置一个provider。为了保证你不会覆盖一个已有的provider（Mist浏览器或安装了MetaMak的浏览器会提供Provider），需要先检查是否web3实例已存在，示例代码如下：</p>
<pre><code class="js">if (typeof web3 !== 'undefined') {
  web3 = new Web3(web3.currentProvider);
} else {
  // set the provider you want from Web3.providers
  web3 = new Web3(new Web3.providers.HttpProvider(&quot;http://localhost:8545&quot;));
}
</code></pre>

<p>成功引入后，你现在可以使用<code>web3</code>对象的<a href="#web3js-api">API</a>了。</p>
<h2 id="_1">使用回调</h2>
<p>由于这套API被设计来与本地的RPC结点交互，所有函数默认使用同步的HTTP的请求。</p>
<p>如果你想发起一个异步的请求。大多数函数允许传一个跟在参数列表后的可选的回调函数来支持异步，回调函数支持<a href="http://fredkschott.com/post/2014/03/understanding-error-first-callbacks-in-node-js/">Error-first回调</a>的风格。</p>
<pre><code class="js">web3.eth.getBlock(48, function(error, result){
    if(!error)
        console.log(JSON.stringify(result));
    else
        console.error(error);
})
</code></pre>

<h2 id="_2">批量请求</h2>
<p>可以允许将多个请求放入队列，并一次执行。</p>
<p><strong>注意</strong>：批量请求并不会更快，在某些情况下，同时发起多个异步请求，也许更快。这里的批量请求主要目的是用来保证请求的串行执行。</p>
<pre><code class="js">var batch = web3.createBatch();
batch.add(web3.eth.getBalance.request('0x0000000000000000000000000000000000000000', 'latest', callback));
batch.add(web3.eth.contract(abi).at(address).balance.request(address, callback2));
batch.execute();
</code></pre>

<h2 id="web3js">web3.js中的大数处理</h2>
<p>如果是一个数据类型的返回结果，通常会得到一个BigNumber对象，因为Javascript不能正确的处理BigNumber，看看下面的例子：</p>
<pre><code class="js">&quot;101010100324325345346456456456456456456&quot;
// &quot;101010100324325345346456456456456456456&quot;
101010100324325345346456456456456456456
// 1.0101010032432535e+38
</code></pre>

<p>所以web3.js依赖<a href="https://github.com/MikeMcl/bignumber.js/">BigNumber Library</a>，且已经自动引入。</p>
<pre><code class="js">var balance = new BigNumber('131242344353464564564574574567456');
// or var balance = web3.eth.getBalance(someAddress);

balance.plus(21).toString(10); // toString(10) converts it to a number string
// &quot;131242344353464564564574574567477&quot;
</code></pre>

<p>下一个例子中，我们会看到，如果有20位以上的浮点值，仍会导致出错。所以推荐尽量让帐户余额以wei为单位，仅仅在需要向用户展示时，才转换为其它单位。</p>
<pre><code class="js">var balance = new BigNumber('13124.234435346456466666457455567456');

balance.plus(21).toString(10); // toString(10) converts it to a number string, but can only show upto 20 digits
// &quot;13145.23443534645646666646&quot; // your number will be truncated after the 20th digit
</code></pre>

<h1 id="web3js-api">Web3.js API列表</h1>
<h2 id="web3_1"><a href="web3/#web3">web3</a></h2>
<ul>
<li><a href="web3/#web3versionapi">web3.version.api</a></li>
<li><a href="web3/#web3versionnode">web3.version.node</a></li>
<li><a href="web3/#web3versionnetwork">web3.version.network</a></li>
<li><a href="web3/#web3versionethereum">web3.version.ethereum</a></li>
<li><a href="web3/#web3versionwhisper">web3.version.whisper</a></li>
<li><a href="web3/#web3isconnected">web3.isConnected</a></li>
<li><a href="web3/#web3setprovider">web3.setProvider</a></li>
<li><a href="web3/#web3currentprovider">web3.currentProvider</a></li>
<li><a href="web3/#web3reset">web3.reset</a></li>
<li><a href="web3/#web3sha3">web3.sha3</a></li>
<li><a href="web3/#web3tohex">web3.toHex</a></li>
<li><a href="web3/#web3toascii">web3.toAscii</a></li>
<li><a href="web3/#web3fromascii">web3.fromAscii</a></li>
<li><a href="web3/#web3todecimal">web3.toDecimal</a></li>
<li><a href="web3/#web3fromdecimal">web3.fromDecimal</a></li>
<li><a href="web3/#web3fromwei">web3.fromWei</a></li>
<li><a href="web3/#web3towei">web3.toWei</a></li>
<li><a href="web3/#web3tobignumber">web3.toBigNumber</a></li>
<li><a href="web3/#web3isaddress">web3.isAddress</a></li>
</ul>
<h2 id="web3net"><a href="web3.net/">web3.net</a></h2>
<ul>
<li><a href="web3.net#web3netlistening">web3.net.listening</a></li>
<li><a href="web3.net#web3netpeercount">web3.net.peerCount</a></li>
</ul>
<h2 id="web3eth"><a href="web3.eth/">web3.eth</a></h2>
<ul>
<li><a href="web3.eth/#web3eth">web3.eth</a></li>
<li><a href="web3.eth/#web3ethdefaultaccount">web3.eth.defaultAccount</a></li>
<li><a href="web3.eth/#web3ethdefaultblock">web3.eth.defaultBlock</a></li>
<li><a href="web3.eth/#web3ethsyncing">web3.eth.syncing</a></li>
<li><a href="web3.eth/#web3ethissyncing">web3.eth.isSyncing</a></li>
<li><a href="web3.eth/#web3ethcoinbase">web3.eth.coinbase</a></li>
<li><a href="web3.eth/#web3ethmining">web3.eth.mining</a></li>
<li><a href="web3.eth/#web3ethhashrate">web3.eth.hashrate</a></li>
<li><a href="web3.eth/#web3ethgasprice">web3.eth.gasPrice</a></li>
<li><a href="web3.eth/#web3ethaccounts">web3.eth.accounts</a></li>
<li><a href="web3.eth/#web3ethblocknumber">web3.eth.blockNumber</a></li>
<li><a href="web3.eth/#web3ethregister">web3.eth.register</a></li>
<li><a href="web3.eth/#web3ethunregister">web3.eth.unRegister</a></li>
<li><a href="web3.eth/#web3ethgetbalance">web3.eth.getBalance</a></li>
<li><a href="web3.eth/#web3ethgetstorageat">web3.eth.getStorageAt</a></li>
<li><a href="web3.eth/#web3ethgetcode">web3.eth.getCode</a></li>
<li><a href="web3.eth/#web3ethgetblock">web3.eth.getBlock</a></li>
<li><a href="web3.eth/#web3ethgetblocktransactioncount">web3.eth.getBlockTransactionCount</a></li>
<li><a href="web3.eth/#web3ethgetuncle">web3.eth.getUncle</a></li>
<li><a href="web3.eth/#web3ethgettransaction">web3.eth.getTransaction</a></li>
<li><a href="web3.eth/#web3ethgettransactionfromblock">web3.eth.getTransactionFromBlock</a></li>
<li><a href="web3.eth/#web3ethgettransactionreceipt">web3.eth.getTransactionReceipt</a></li>
<li><a href="web3.eth/#web3ethgettransactioncount">web3.eth.getTransactionCount</a></li>
<li><a href="web3.eth/#web3ethsendtransaction">web3.eth.sendTransaction</a></li>
<li><a href="web3.eth/#web3ethsendrawtransaction">web3.eth.sendRawTransaction</a></li>
<li><a href="web3.eth/#web3ethsign">web3.eth.sign</a></li>
<li><a href="web3.eth/#web3ethcall">web3.eth.call</a></li>
<li><a href="web3.eth/#web3ethestimategas">web3.eth.estimateGas</a></li>
<li><a href="web3.eth/#web3ethfilter">web3.eth.filter</a></li>
<li><a href="web3.eth/#web3ethcontract">web3.eth.contract</a></li>
<li><a href="web3.eth/#contract-methods">Contract Methods</a></li>
<li><a href="web3.eth/#contract-events">Contract Events</a></li>
<li><a href="web3.eth/#contract-allevents">Contract allEvents</a></li>
<li><a href="web3.eth/#web3ethgetcompilers">web3.eth.getCompilers</a></li>
<li><a href="web3.eth/#web3ethcompilesolidity">web3.eth.compile.solidity</a></li>
<li><a href="web3.eth/#web3ethcompilelll">web3.eth.compile.lll</a></li>
<li><a href="web3.eth/#web3ethcompileserpent">web3.eth.compile.serpent</a></li>
<li><a href="web3.eth/#web3ethnamereg">web3.eth.namereg</a></li>
<li><a href="web3.eth/#web3ethsendibantransaction">web3.eth.sendIBANTransaction</a></li>
<li><a href="web3.eth/#web3ethiban">web3.eth.iban</a></li>
<li><a href="web3.eth/#web3ethibanfromaddress">web3.eth.iban.fromAddress</a></li>
<li><a href="web3.eth/#web3ethibanfrombban">web3.eth.iban.fromBban</a></li>
<li><a href="web3.eth/#web3ethibancreateindirect">web3.eth.iban.createIndirect</a></li>
<li><a href="web3.eth/#web3ethibanisvalid">web3.eth.iban.isValid</a></li>
<li><a href="web3.eth/#web3ethibanisdirect">web3.eth.iban.isDirect</a></li>
<li><a href="web3.eth/#web3ethibanisindirect">web3.eth.iban.isIndirect</a></li>
<li><a href="web3.eth/#web3ethibanchecksum">web3.eth.iban.checksum</a></li>
<li><a href="web3.eth/#web3ethibaninstitution">web3.eth.iban.institution</a></li>
<li><a href="web3.eth/#web3ethibanclient">web3.eth.iban.client</a></li>
<li><a href="web3.eth/#web3ethibanaddress">web3.eth.iban.address</a></li>
<li><a href="web3.eth/#web3ethibantostring">web3.eth.iban.toString</a></li>
</ul>
<h2 id="web3db"><a href="web3.db/">web3.db</a></h2>
<ul>
<li><a href="web3.db#web3dbputstring">web3.db.putString</a></li>
<li><a href="web3.db#web3dbgetstring">web3.db.getString</a></li>
<li><a href="web3.db#web3dbputhex">web3.db.putHex</a></li>
<li><a href="web3.db#web3dbgethex">web3.db.getHex</a></li>
</ul>
<h2 id="web3shh"><a href="web3.shh/">web3.shh</a></h2>
<ul>
<li><a href="web3.shh#web3shhpost">web3.shh.post</a></li>
<li><a href="web3.shh#web3shhnewidentity">web3.shh.newIdentity</a></li>
<li><a href="web3.shh#web3shhhasidentity">web3.shh.hasIdentity</a></li>
<li><a href="web3.shh#web3shhnewgroup">web3.shh.newGroup</a></li>
<li><a href="web3.shh#web3shhaddtogroup">web3.shh.addToGroup</a></li>
<li><a href="web3.shh#web3shhfilter">web3.shh.filter</a></li>
</ul></div>
        </div>

        <footer class="col-md-12">
            <hr>
            <p>Documentation built with <a href="https://www.mkdocs.org/">MkDocs</a>.</p>
        </footer>
        <script>
            var base_url = ".",
                shortcuts = {"help": 191, "next": 78, "previous": 80, "search": 83};
        </script>
        <script src="js/base.js" defer></script>
        <script src="search/main.js" defer></script>

        <div class="modal" id="mkdocs_search_modal" tabindex="-1" role="dialog" aria-labelledby="Search Modal" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="exampleModalLabel">Search</h4>
            </div>
            <div class="modal-body">
                <p>
                    From here you can search these documents. Enter
                    your search terms below.
                </p>
                <form role="form">
                    <div class="form-group">
                        <input type="text" class="form-control" placeholder="Search..." id="mkdocs-search-query" title="Type search term here">
                    </div>
                </form>
                <div id="mkdocs-search-results"></div>
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div><div class="modal" id="mkdocs_keyboard_modal" tabindex="-1" role="dialog" aria-labelledby="Keyboard Shortcuts Modal" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="exampleModalLabel">Keyboard Shortcuts</h4>
            </div>
            <div class="modal-body">
              <table class="table">
                <thead>
                  <tr>
                    <th style="width: 20%;">Keys</th>
                    <th>Action</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td class="help shortcut"><kbd>?</kbd></td>
                    <td>Open this help</td>
                  </tr>
                  <tr>
                    <td class="next shortcut"><kbd>n</kbd></td>
                    <td>Next page</td>
                  </tr>
                  <tr>
                    <td class="prev shortcut"><kbd>p</kbd></td>
                    <td>Previous page</td>
                  </tr>
                  <tr>
                    <td class="search shortcut"><kbd>s</kbd></td>
                    <td>Search</td>
                  </tr>
                </tbody>
              </table>
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div>

    </body>
</html>

<!--
MkDocs version : 1.0.2
Build Date UTC : 2018-09-13 10:10:36
-->
